home *** CD-ROM | disk | FTP | other *** search
- Path: news.microsoft.com!news
- From: a-cnadc@microsoft.com (Dann Corbit)
- Newsgroups: comp.lang.c
- Subject: Re: Help, best way to compare doubles
- Date: 16 Jan 1996 17:36:37 GMT
- Organization: Microsoft Corporation
- Message-ID: <4dgnn5$em@news.microsoft.com>
- References: <4da169$mm4@mercury.IntNet.net>
- NNTP-Posting-Host: 157.57.171.202
- Mime-Version: 1.0
- X-Newsreader: WinVN 0.93.14
-
- In article <4da169$mm4@mercury.IntNet.net>, jtomich@IntNet.net says...
- >
- >Looking for a way to compare doubles for equality.
- >
- A very dangerous practice, as computing the same result two different
- ways may not have exactly the same result. Sometimes a test such as:
- abs( double1 - double2 ) < dEpsilon
- is used, but this technique is not always valid. Suppose, for instance,
- that double1 and double2 are enormous numbers. Though they might differ
- only in the least significant bit, the difference could still be huge.
- Sometimes a relative error is calculated, such as:
- abs( double1 - double2 )/max( double1, double2 ) < dEpsilon
- but that has a weakness if double1 and double2 are vastly different values.
- The best solution will vary greatly, depending on what you actually want
- to accomplish.
-
- The abridged FAQ says:
-
- 14.5: What's a good way to check for "close enough" floating-point
- equality?
-
- A: The best way is to use an accuracy threshold which is relative
- to the magnitude of the numbers being compared.
-
- and in general, that's pretty good advice. But in any case, you should
- do some careful analysis to make sure that the code accomplishes what
- you want it to.
- --
- The opinions expressed in this message are my own personal views
- and do not reflect the official views of Microsoft Corporation.
-
-